package com.priox.sigar.application.job.mappers;


import com.priox.sigar.client.dto.SociedadDTO;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;

/**
 * User: Matrix
 * Date: 10/02/13
 */
@Component
public class SociedadFieldSetMapper implements FieldSetMapper<SociedadDTO> {
    private static final Logger LOGGER = LoggerFactory.getLogger(SociedadFieldSetMapper.class);
    /*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet
     * (org.springframework.batch.item.file.transform.FieldSet)
     */
    @Override
    public SociedadDTO mapFieldSet(FieldSet fieldSet) throws BindException {
        SociedadDTO result = new SociedadDTO();
        try {
            if(NumberUtils.isNumber(fieldSet.readString(1))){
                result.setSociedadId(fieldSet.readString(1));
                result.setNombre(fieldSet.readString(2));
                result.getGrupoEmpresarial().setNombre(fieldSet.readString(0));
            }
        } catch (Exception ex) {
            LOGGER.error("error al leer la sociedad ",ex);
        }

        return result;
    }
}

